﻿package crochet.core;

/*
 Copyright 2011 Intuitive Solutions

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

import java.util.HashMap;

import crochet.core.policy.IExecutionPolicy;

public class CrochetManager implements IScheduler, IDisposable {
	private static CrochetManager cmInstance = null;

	private HashMap<SchedulerType, HashMap<String, IScheduler>> dSchedulers;

	public CrochetManager() {
		dSchedulers = new HashMap<SchedulerType, HashMap<String, IScheduler>>();
	}

	public static CrochetManager getInstance() {
		if (cmInstance == null) {
			synchronized (CrochetManager.class) {
				if (cmInstance == null) {
					cmInstance = new CrochetManager();
				}
			}
		}

		return cmInstance;
	}

	public IExecutionPolicy getCloneableExecutionPolicy() {
		throw new UnsupportedOperationException();
	}

	public void schedule(IExecutionPolicy epExecutionPolicy) throws Exception {
		SchedulerAttribute saSchedulerAttribute = epExecutionPolicy.getClass()
				.getAnnotation(SchedulerAttribute.class);

		schedule(saSchedulerAttribute.schedulerType(),
				saSchedulerAttribute.name(), epExecutionPolicy);
	}

	public void schedule(ISchedulerDelegate sdSchedulerDelegate)
			throws Exception {
		schedule(sdSchedulerDelegate, null);
	}

	public void schedule(ISchedulerDelegate sdSchedulerDelegate,
			IExceptionHandlerDelegate ehdExceptionHandlerDelegate)
			throws Exception {
		SchedulerAttribute saSchedulerAttribute = sdSchedulerDelegate
				.getClass().getMethod("execute", IScheduler.class)
				.getAnnotation(SchedulerAttribute.class);

		schedule(saSchedulerAttribute.schedulerType(),
				saSchedulerAttribute.name(), sdSchedulerDelegate,
				ehdExceptionHandlerDelegate);
	}

	public void schedule(SchedulerType stSchedulerType, String sName,
			IExecutionPolicy epExecutionPolicy) throws Exception {
		getScheduler(stSchedulerType, sName).schedule(epExecutionPolicy);
	}

	public void schedule(SchedulerType stSchedulerType, String sName,
			ISchedulerDelegate sdSchedulerDelegate) throws Exception {
		schedule(stSchedulerType, sName, sdSchedulerDelegate, null);
	}

	public void schedule(SchedulerType stSchedulerType, String sName,
			ISchedulerDelegate sdSchedulerDelegate,
			IExceptionHandlerDelegate ehdExceptionHandlerDelegate)
			throws Exception {
		IScheduler sScheduler = getScheduler(stSchedulerType, sName);

		sScheduler.schedule(sScheduler.getCloneableExecutionPolicy().clone(
				sdSchedulerDelegate, ehdExceptionHandlerDelegate));
	}

	public IScheduler getScheduler(SchedulerType stSchedulerType, String sName) {
		IScheduler sScheduler = null;

		if (dSchedulers.containsKey(stSchedulerType)
				&& dSchedulers.get(stSchedulerType).containsKey(sName)) {
			sScheduler = dSchedulers.get(stSchedulerType).get(sName);
		} else {
			synchronized (cmInstance) {
				if (dSchedulers.containsKey(stSchedulerType)
						&& dSchedulers.get(stSchedulerType).containsKey(sName)) {
					sScheduler = dSchedulers.get(stSchedulerType).get(sName);
				} else {
					HashMap<String, IScheduler> dSchedulerByType = null;

					if (dSchedulers.containsKey(stSchedulerType)) {
						dSchedulerByType = dSchedulers.get(stSchedulerType);
					} else {
						dSchedulerByType = new HashMap<String, IScheduler>();

						dSchedulers.put(stSchedulerType, dSchedulerByType);
					}

					sScheduler = SchedulerFactory.getInstance()
							.CreateScheduler(stSchedulerType, sName);

					dSchedulerByType.put(sName, sScheduler);
				}
			}
		}

		return sScheduler;
	}

	public IScheduler CreateScheduler(SchedulerType stSchedulerType,
			Object... oaParams) {
		IScheduler sScheduler = null;
		HashMap<String, IScheduler> dSchedulerByType = null;

		if (dSchedulers.containsKey(stSchedulerType)) {
			dSchedulerByType = dSchedulers.get(stSchedulerType);
		} else {
			dSchedulerByType = new HashMap<String, IScheduler>();

			dSchedulers.put(stSchedulerType, dSchedulerByType);
		}

		sScheduler = SchedulerFactory.getInstance().CreateScheduler(
				stSchedulerType, oaParams);

		dSchedulerByType.put((String) oaParams[0], sScheduler);

		return sScheduler;
	}

	public void dispose() {
		if (dSchedulers.size() > 0) {
			for (HashMap<String, IScheduler> dSchedulerType : dSchedulers
					.values()) {
				if (dSchedulerType.size() > 0) {
					for (IScheduler sScheduler : dSchedulerType.values()) {
						sScheduler.dispose();
					}
				}
			}
		}

		dSchedulers = null;
		cmInstance = null;
	}

}
